package com.putao.album.message;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.putao.album.R;
import com.putao.album.base.PtBaseAdapter;
import com.putao.album.child.bean.RelationItem;
import com.putao.album.db.tables.InMessage;
import com.putao.album.util.BitmapHelper;
import com.putao.album.util.DateUtil;

import java.util.List;

/**
 * Created by jidongdong on 15/5/14.
 */
public class InMessageAdapter extends PtBaseAdapter<InMessage> {
    DisplayImageOptions options;

    public InMessageAdapter(Context context, List<InMessage> datalist) {
        super(context, datalist);
        options = new DisplayImageOptions.Builder().showImageOnLoading(BitmapHelper.getLoadingDrawable()).showImageOnFail(BitmapHelper.getLoadingDrawable())
                .considerExifParams(true).cacheInMemory(true).cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_message_item, null);
            holder.relation_icon = (ImageView) convertView.findViewById(R.id.relation_icon);
            holder.content = (TextView) convertView.findViewById(R.id.message_content);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.readed = (ImageView) convertView.findViewById(R.id.readed);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.action = (Button) convertView.findViewById(R.id.action);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        InMessage item = getItem(position);
        setData(holder, item);
        return convertView;
    }

    /**
     * 设置UI数据
     *
     * @param holder
     * @param item
     */
    private void setData(ViewHolder holder, InMessage item) {
        String relation_icon = item.getHeader_photo();
        int resId = RelationItem.Tag.getResIdByTag(relation_icon);
        if (resId > 0) {
            holder.relation_icon.setImageResource(resId);
        }
        holder.content.setText(item.getMessage_content());
        holder.time.setText(dealMessageTime(item.getCreatetime()));
        boolean isReaded = (item.getIsread() == 1);
        if (isReaded) {
            holder.readed.setVisibility(View.GONE);
        } else {
            holder.readed.setVisibility(View.VISIBLE);
            holder.readed.setImageBitmap(BitmapHelper.getCircleBitmap(Color.RED, 20));
        }
        String icon = item.getBaby_picture();
        if (icon != null) {
            holder.icon.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(item.getBaby_picture(), holder.icon, options);
        } else {
            holder.icon.setVisibility(View.GONE);
        }
    }

    /**
     * 处理消息时间格式
     *
     * @param createTime
     * @return
     */
    String dealMessageTime(long createTime) {
        String date_time = "";
        long currentTime = System.currentTimeMillis();
        if (DateUtil.getYear(createTime) < DateUtil.getYear(currentTime)) {
            date_time = DateUtil.getDate(createTime, "yy.MM.dd");
        } else {
            if (DateUtil.getDay(createTime) == DateUtil.getDay(currentTime)) {
                date_time = mContext.getString(R.string.today_string);
            } else {
                date_time = DateUtil.getDate(createTime, "MM.dd");
            }
        }
        return date_time;
    }

    class ViewHolder {
        public ImageView relation_icon;
        public TextView content;
        public TextView time;
        public ImageView readed;
        public ImageView icon;
        public Button action;
    }
}
